package com.yeyaomai.dksns.flowis.servlet;

import java.io.StringWriter;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.query.Order;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yeyaomai.dksns.flowis.domain.flowisdetail.FlowisQueryOrder;
import com.yeyaomai.dksns.flowis.domain.flowisdetail.FlowisStatDetailDTO;
import com.yeyaomai.dksns.flowis.domain.flowisdetail.LoginProfile;
import com.yeyaomai.dksns.flowis.domain.flowisdetail.LoginSession;
import com.yeyaomai.dksns.flowis.service.FlowisQueryService;
import com.yeyaomai.dksns.flowis.service.LoginProfileService;
import com.yeyaomai.dksns.flowis.service.LoginSessionService;
import com.yeyaomai.dksns.flowis.servlet.dto.AjaxResult;
import com.yeyaomai.dksns.flowis.util.Globals;
import com.yeyaomai.dksns.flowis.util.VelocityToolDateUtils;

/**
 * @author dingtao@yeyaomai.com
 */
@Controller
@RequestMapping("/flowis/*")
public class FlowisQueryDetailController {

	//private static Logger logger = LoggerFactory.getLogger(FlowisQueryDetailController.class);

	@Autowired
	FlowisQueryService flowisQueryService;
	
	@Autowired
	private LoginProfileService loginProfileService;
	
	@Autowired
	private LoginSessionService loginSessionService;

	private static ObjectMapper objectMapper = new ObjectMapper();

	@RequestMapping(value="query")
	public String flowisQuery(HttpServletRequest request,
			HttpServletResponse response, FlowisQueryOrder order,
			ModelMap modelMap) {
		order.addSort("access_date", Order.DESCENDING.toString());
		String type = request.getParameter("type");
		Page<FlowisStatDetailDTO> result = flowisQueryService.getSplit(order);
		Integer month = Integer.valueOf("-"+Globals.FORMAT_TYPE_D.format(new Date()))+1;
		String day = order.getDay();
        String startTime = VelocityToolDateUtils.getDay(Integer.valueOf(day));
        String endTime = VelocityToolDateUtils.getDay(Integer.valueOf(day));
        if(day.equals(String.valueOf(month)) || day.equals("-1") || day.equals("0") || day.equals("-29"))
        {
        	endTime = Globals.FORMAT_TYPE_A.format(new Date());
        }
        
		modelMap.addAttribute("result", result);
		modelMap.addAttribute("startTime", startTime);
		modelMap.addAttribute("endTime", endTime);
		modelMap.addAttribute("order", order);
		modelMap.addAttribute("currentDay", order.getDay());
		modelMap.addAttribute("currentHour", order.getHour());
		modelMap.addAttribute("type", type);
		modelMap.addAttribute("month", month);
		modelMap.addAttribute("ip", order.getIp());
		modelMap.addAttribute("city", order.getAccess_city());
		return "visit-detail";

	}

	/**
	 * 离线用户查询
	 * @param request
	 * @param response
	 * @param order
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value="loginprofile")
	public String LoginProfileQuery(HttpServletRequest request,HttpServletResponse response, FlowisQueryOrder order,
			ModelMap modelMap) {
		order.addSort("end_time", Order.DESCENDING.toString());
		Page<LoginProfile> result = loginProfileService.getSplit(order);
		Integer totalUser = loginProfileService.getTotalSizeByUserInfo();
		modelMap.addAttribute("result", result);
		modelMap.addAttribute("order", order);
		modelMap.addAttribute("username", order.getUsername());
		modelMap.addAttribute("user_dkcode", order.getUser_dkcode());
		modelMap.addAttribute("totalUser", totalUser);
		return "login-profile";
	}
	
	/**
	 * 在线用户查询
	 * @param request
	 * @param response
	 * @param order
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value="loginsession")
	public String LoginSessionQuery(HttpServletRequest request,HttpServletResponse response, FlowisQueryOrder order,
			ModelMap modelMap) {
		order.addSort("start_time", Order.DESCENDING.toString());
		Page<LoginSession> result = loginSessionService.getSplit(order);
		modelMap.addAttribute("result", result);
		modelMap.addAttribute("order", order);
		modelMap.addAttribute("username", order.getUsername());
		modelMap.addAttribute("user_dkcode", order.getUser_dkcode());
		return "login-session";
	}
	
	/**
	 * 
	 * 
	 * @param request
	 * @param response
	 * @param ip
	 * @return
	 */
	// TODO 前端 约定 json 数据格式
	@RequestMapping(value="ajax")
	@ResponseBody
	public String flowisQuery(HttpServletRequest request,
			HttpServletResponse response, String ip) {

		AjaxResult jsonresult = new AjaxResult();

		if (StringUtils.isBlank(ip)) {

			jsonresult.setResult(false);
		} else {

			// LinkedHashMap<String, String> queryArgs = new
			// LinkedHashMap<String, String>();
			// queryArgs.put("ip", ip);
			// TODO 查询mongodb 数据量大性能低 或者基于cookie
			// List<FlowisStatDetailDTO> dtos = flowisQueryService
			// .getDescbyCondition(queryArgs);

//			Cookie[] cookies = request.getCookies();
//			for (Cookie cookie : cookies) {
//
//			}

		}

		StringWriter sw = new StringWriter();
		try {
			objectMapper.writeValue(sw, jsonresult);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sw.toString();
	}

}
